<template>
  <div>
    <ymm-content title="学生管理">
      <template slot="toolbar">
        <ymm-toolbar :show-add="false" @handleInput="onInput"/>
      </template>
      <template>
        <stu-table :students="students" @show="onInfo" @deleteStu="onDelete"/>
        <ymm-pagination :total="total" @sizeChange="onSize" @numChange="onNum"/>
      </template>
    </ymm-content>
    <info-dialog :is-show="show" :student="student" @confirm="show=false"/>
  </div>
</template>

<script>
import YmmContent from "@/components/common/ymm-content";
import YmmToolbar from "@/components/common/ymm-toolbar";
import YmmPagination from "@/components/common/ymm-pagination";
import StuTable from "@/views/doctor/student/stu-table";
import InfoDialog from "@/views/doctor/student/info-dialog";
import {getStudentList} from "@/api/doctor-student";
import {deleteUser} from "@/api/system-user";

export default {
  data() {
    return {
      students: [],
      total: 0,
      pageNum: 1,
      pageSize: 10,
      name: '',
      student: {},
      show: false
    }
  },
  components: {YmmToolbar, YmmPagination, YmmContent, StuTable, InfoDialog},
  methods: {
    /* index事件*/
    getStudents() {
      getStudentList(this.pageNum, this.pageSize, this.name).then(res => {
        this.students = res.rows
        this.total = res.total
      }).catch(err => {
        this.$message.error('获取学生列表失败')
      })
    },
    /*table事件*/
    onInfo(student) {
      this.student = student
      this.show = true
    },
    onDelete(id) {
      this.$confirm('此操作将永久删除该学生, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteUser(id).then(res => {
          this.$message.success('删除成功')
          this.getStudents()
        }).catch(err => {
          console.log(err)
          this.$message.error('删除失败')
        })
      }).catch(() => {
        this.$message.info('已取消删除')
      });
    },
    /*ToolBar事件*/
    onInput(val) {
      this.name = val
      this.getStudents()
    },
    /*分页事件*/
    onSize(val) {
      this.pageSize = val
      this.getStudents()
    },
    onNum(val) {
      this.pageNum = val
      this.getStudents()
    },
  },
  created() {
    this.getStudents()
  }
}
</script>

<style scoped>

</style>
